package org.r.items.tally.db.remote.consumer.user;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.r.items.tally.consumer.api.user.TallyUserDetailsRemote;
import org.r.items.tally.consumer.dto.UserInfo;
import org.r.items.tally.db.service.org.RoleService;
import org.r.items.tally.db.service.user.UserInfoService;
import org.r.items.tally.table.org.RoleTable;
import org.r.items.tally.table.user.UserInfoTable;
import org.r.items.tally.table.user.UserRoleTable;

@DubboService
public class TallyUserDetailsRemoteImpl implements TallyUserDetailsRemote {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private RoleService roleService;

    @Override
    public UserInfo getUserInfoByName(String username) {
        UserInfo userInfo = userInfoService.selectJoinOne(UserInfo.class,
                new MPJLambdaWrapper<UserInfoTable>()
                        .selectAs(UserInfoTable::getId, UserInfo::getUserId)
                        .select(UserInfoTable::getUsername, UserInfoTable::getEmail, UserInfoTable::getCellphoneNumber, UserInfoTable::getPassword)
                        .select(UserInfoTable::getGender, UserInfoTable::getSignature, UserInfoTable::getArea, UserInfoTable::getStatus)
                        .eq(UserInfoTable::getUsername, username)
                        .eq(UserInfoTable::getDeleted, 0)
        );

        RoleTable roleTable = roleService.selectJoinOne(RoleTable.class,
                new MPJLambdaWrapper<RoleTable>()
                        .select(RoleTable::getId, RoleTable::getCode, RoleTable::getName)
                        .leftJoin(UserRoleTable.class, UserRoleTable::getRoleId, RoleTable::getId)
                        .eq(UserRoleTable::getUserId, userInfo.getUserId())
        );

        return userInfo;
    }

    @Override
    public UserInfo getUserInfoByCellPhoneNum(String cellPhoneNum) {
        UserInfo userInfo = userInfoService.selectJoinOne(UserInfo.class,
                new MPJLambdaWrapper<UserInfoTable>()
                        .selectAs(UserInfoTable::getId, UserInfo::getUserId)
                        .select(UserInfoTable::getUsername, UserInfoTable::getEmail, UserInfoTable::getCellphoneNumber, UserInfoTable::getPassword)
                        .select(UserInfoTable::getGender, UserInfoTable::getSignature, UserInfoTable::getArea, UserInfoTable::getStatus)
                        .eq(UserInfoTable::getCellphoneNumber, cellPhoneNum)
                        .eq(UserInfoTable::getDeleted, 0)
        );


        RoleTable roleTable = roleService.selectJoinOne(RoleTable.class,
                new MPJLambdaWrapper<RoleTable>()
                        .select(RoleTable::getId, RoleTable::getCode, RoleTable::getName)
                        .leftJoin(UserRoleTable.class, UserRoleTable::getRoleId, RoleTable::getId)
                        .eq(UserRoleTable::getUserId, userInfo.getUserId())
        );
        return null;
    }

    @Override
    public UserInfo getUserInfoByEmail(String email) {
        UserInfo userInfo = userInfoService.selectJoinOne(UserInfo.class,
                new MPJLambdaWrapper<UserInfoTable>()
                        .selectAs(UserInfoTable::getId, UserInfo::getUserId)
                        .select(UserInfoTable::getUsername, UserInfoTable::getEmail, UserInfoTable::getCellphoneNumber, UserInfoTable::getPassword)
                        .select(UserInfoTable::getGender, UserInfoTable::getSignature, UserInfoTable::getArea, UserInfoTable::getStatus)
                        .eq(UserInfoTable::getEmail, email)
                        .eq(UserInfoTable::getDeleted, 0)
        );

        RoleTable roleTable = roleService.selectJoinOne(RoleTable.class,
                new MPJLambdaWrapper<RoleTable>()
                        .select(RoleTable::getId, RoleTable::getCode, RoleTable::getName)
                        .leftJoin(UserRoleTable.class, UserRoleTable::getRoleId, RoleTable::getId)
                        .eq(UserRoleTable::getUserId, userInfo.getUserId())
        );
        return null;
    }
}
